package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.MetricDefineMapper;
import com.hllxd.hism.star.basic.service.model.metric.MetricDefineEntity;
import com.hllxd.hism.star.basic.service.model.UserFocusMetricEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@Service("metricDefineService")
public class MetricDefineServiceImpl implements MetricDefineService {

    @Autowired
    private MetricDefineMapper metricDefineMapper;


    @Override
    public MetricDefineEntity getMetric(Long id) {
        return metricDefineMapper.getMetric(id);
    }

    @Override
    public Long setMetric(MetricDefineEntity entity) {
        log.info("set param: {}", JSON.toJSONString(entity));
        if (entity.id == null || entity.id == 0) {
            log.info("will insert entity.");
            return metricDefineMapper.insertMetric(entity);
        } else {
            log.info("will update entity.");
            return metricDefineMapper.updateMetric(entity);
        }
    }

    @Override
    public List<MetricDefineEntity> listMetricRouter(String bizArea) {
        return metricDefineMapper.listMetricRouter(bizArea);
    }

    @Override
    public List<MetricDefineEntity> listMetric(String bizArea, String topic) {
        return metricDefineMapper.listMetric(bizArea, topic);
    }
    @Override
    public List<MetricDefineEntity> searchMetric(String bizArea,String search) {
        if(search.isEmpty()) {
            return new ArrayList<>();
        }
        return metricDefineMapper.searchMetric(bizArea,"%"+search+"%");
    }
    @Override
    public Boolean deleteMetric(String ids) {
        return metricDefineMapper.deleteMetric(ids);
    }

    @Override
    public long focusMetric(String userId, Long id) {
        return metricDefineMapper.insertUserFocusMetric(userId, id);
    }

    @Override
    public long cancelSubscribe(String userId, Long id) {
        return metricDefineMapper.deleteUserFocusMetric(userId, id);
    }

    @Override
    public List<UserFocusMetricEntity> listUserSubscribe(String userId) {
        return metricDefineMapper.selectUserSubscribeByUserId(userId);
    }
}
